/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package utils.properties;

import constants.PropertyConstants;
import processing.objects.Server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Holds username and password properties for servers
 *
 * @author rymek
 *         User: Rymek
 *         Date: 24.07.11
 *         Time: 18:24
 */
public class ServerProperties extends Properties {

    /**
     * Constructor.
     * Loads server properties.
     *
     * @throws IOException when cannot access server properties file
     */
    public ServerProperties() throws IOException {
        this.importServers();
    }

    /**
     * Sets credentials for specific server
     *
     * @param server Server to store credentials for
     */
    public void storeCredentials(Server server) {
        this.setProperty(server.getTnsName().toLowerCase() + PropertyConstants.PROP_SERVER__USERNAME_SUFFIX, server.getUserName());
        this.setProperty(server.getTnsName().toLowerCase() + PropertyConstants.PROP_SERVER_PASSWORD_SUFFIX, server.getUserPass());
    }

    /**
     * Gets username for specific server
     *
     * @param server server object
     * @return server bound username
     */
    public String getUsername(Server server) {
        return this.getProperty(server.getTnsName().toLowerCase() + PropertyConstants.PROP_SERVER__USERNAME_SUFFIX);
    }

    /**
     * Gets password for specific server
     *
     * @param server server object
     * @return server bound password
     */
    public String getPassword(Server server) {
        return this.getProperty(server.getTnsName().toLowerCase() + PropertyConstants.PROP_SERVER_PASSWORD_SUFFIX);
    }

    /**
     * Exports ServerProperties to a file
     *
     * @throws IOException thrown when export fails
     */
    public void exportServers() throws IOException {

        if (!(new File(PropertyConstants.CFG_CONFIG_DIR)).exists()) {
            if (!new File(PropertyConstants.CFG_CONFIG_DIR).mkdir()) throw new IOException();
        }

        if (!(new File(PropertyConstants.CFG_SERVERS_CFG_FILEPATH)).exists())
            if (!new File(PropertyConstants.CFG_SERVERS_CFG_FILEPATH).createNewFile()) throw new IOException();

        this.store(new FileOutputStream(PropertyConstants.CFG_SERVERS_CFG_FILEPATH), null);
    }

    /**
     * Imports ServerProperties from file
     *
     * @throws IOException thrown when import fails
     */
    void importServers() throws IOException {
        if ((new File(PropertyConstants.CFG_SERVERS_CFG_FILEPATH)).exists()) {
            this.load(new FileInputStream(PropertyConstants.CFG_SERVERS_CFG_FILEPATH));
        }
    }
}
